package com.e.Dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.OrientationEventListener;
import android.widget.Button;

import com.e.Pojo.Order;
import com.e.Pojo.User;

import java.util.ArrayList;
import java.util.List;

public class UserDao {

    private SQLiteDatabase db;
    public SQLiteDatabase getDb(){
        return db;
    }

    public UserDao(SQLiteOpenHelper dbHelper){
        db = dbHelper.getWritableDatabase();
    }

    //通过查询
    public List<User> execQuery(final String strSQL){
        List<User> list = new ArrayList<>();
        try {
            Cursor cursor = db.rawQuery(strSQL, null);
            cursor.moveToFirst();      //将游标移动到开头
            list.clear();             //清空容器
            while (!cursor.isAfterLast()) {              //如果游标不是最后一项纪录
                User user = new User();
                user.setSno(cursor.getString(0));
                user.setSname(cursor.getString(1));
                user.setPhone(cursor.getString(2));
                user.setCollege(cursor.getString(3));
                user.setSdept(cursor.getString(4));
                user.setSsex(cursor.getString(5));
                user.setPassword(cursor.getString(6));
                list.add(user);
                cursor.moveToNext();    //将游标移动到下一个
            }

        }catch (RuntimeException e) {
            e.printStackTrace();
        }
        return list;
    }


    //插入，删除，更新方法
    public boolean execSQL(final String strSQL){
        db.beginTransaction();
        try{
            db.execSQL(strSQL);
            db.setTransactionSuccessful();       //设置事物成功
            return true;
        }catch(RuntimeException e){
            e.printStackTrace();
            return false;
        }finally {
            db.endTransaction();             //结束事物
        }
    }

    //登录判断
    public boolean findUserByName(String sno,String password){
       String str  = "select * from user where sno = ? and password = ?";
       String [] selectionArg = new String[]{sno,password};
       Cursor cursor = db.rawQuery(str,selectionArg);
       if(cursor.moveToNext()){
           cursor.close();
           return true;
       }else {
           return false;
       }
    }

    //查询当前用户信息
    public User findUserBySno(String sno){
        String str = "select * from user where sno = ?";
        String[] selectionArg = new String[]{sno};
        Cursor cursor = db.rawQuery(str,selectionArg);
        cursor.moveToFirst();
        User u = new User();
        u.setSno(cursor.getString(0)) ;
        u.setSname(cursor.getString(1)); ;
        u.setPhone(cursor.getString(2)); ;
        u.setCollege(cursor.getString(3)); ;
        u.setSdept(cursor.getString(4)); ;
        u.setSsex(cursor.getString(5)); ;
        u.setPassword(cursor.getString(6)); ;
        return u;
    }
    //修改密码
    public Boolean updatePassword(String password,String sno){
        String sql = "update user set password = '"+password+"' where sno = '"+sno+"' ";
        return execSQL(sql);

    }


    //判断当前车次是否已经预约过
    public Boolean findCheckOrder(String sno,String start,String date){
        String sql = "select * from ticket where sno = ? and start = ? and date1 = ?";
        String[] selectionArg = new String[]{sno,start,date};
        Cursor cursor = db.rawQuery(sql,selectionArg);
        if(cursor.moveToNext()){
            cursor.close();
            return false;
        }else {
            return true;
        }
    }

    //添加预约信息
    public Boolean insertOrder(Order order){
        String sql = "insert into ticket(sno,sname,date1,start,last,phone) values ('"+order.getSno()+"','"+order.getSname()+"'," +
                "'"+order.getDate1()+"','"+order.getStart()+"','"+order.getLast()+"','"+order.getPhone()+"')";
        System.out.println("======>>>>>>>>>>>>"+order.toString());
        return execSQL(sql);
    }

    //查看预约信息
    public List<Order> findTicketBySno(String sno){
        List<Order> list = new ArrayList<>();
        String sql = "select * from ticket where sno = ?";
        String[] selectionArg = new String[]{sno};
        Cursor cursor = db.rawQuery(sql,selectionArg);
        cursor.moveToFirst();
        list.clear();
        while (!cursor.isAfterLast()){
            Order order = new Order();
            order.setId(cursor.getInt(0));
            order.setSno(cursor.getString(1));
            order.setSname(cursor.getString(2));
            order.setDate1(cursor.getString(3));
            order.setStart(cursor.getString(4));
            order.setLast(cursor.getString(5));
            order.setPhone(cursor.getString(6));
            list.add(order);
            cursor.moveToNext();
        }
        cursor.close();
        return list;
    }

    //取消预约
    public Boolean deleteFindById(Integer id){
        String s = "delete from ticket where id = '"+id+"'";
        return execSQL(s);
    }


}
